package com.lanou.musicbox.fragment.seekmusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.seekmusic.EnterTheSeekMusic_PlaylistAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Playlist;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/24.
 * 进入搜索_歌单页面
 */
public class EnterTheSeekMusic_PlaylistFragment extends BaseFragment {
    private List<Playlist.DataEntity> data;
    private ListView playlistListView;
    private EnterTheSeekMusic_PlaylistAdapter adapter;
    private VolleySingleton volleySingleton;
    private String newUrl;

    public static EnterTheSeekMusic_PlaylistFragment passData(Context mContext, String str) {
        EnterTheSeekMusic_PlaylistFragment fragment = new EnterTheSeekMusic_PlaylistFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.entertheseekmusic_playlist, null);
        playlistListView = (ListView) view.findViewById(R.id.playlistListView);
        playlistListView.setCacheColorHint(1);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        newUrl = Contant.Url.URL_Seek_Songs_Menu_first + url +Contant.Url.URL_Seek_Songs_Menu_end;

        initData();
        return view;
    }

    private void initData() {
        data = new ArrayList<>();
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Playlist p = gson.fromJson(response, Playlist.class);
                data = p.getData();
                adapter = new EnterTheSeekMusic_PlaylistAdapter(data, getActivity());
                playlistListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }
}
